<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/21
 * Time: 20:26
 */

namespace App\Http\Controllers\Bowen;

use App\Http\Classes\Reply;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Classes\Log;

class ReplyController extends Controller
{
    public function sReplys(Request $request){
        try{
            if($request->has('comment_id')){
                $comment_id = $request->input('comment_id');
                $data = [];
                $data['replys'] = Reply::getReplys($comment_id);
                if($data['replys'] != null){
                    return $this->response(200,"获取成功",$data);
                }
                else{
                    return $this->response(304,"没有数据",$data);
                }
            }
            else{
                return $this->response(503,"缺少必要参数！",null);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }

    public function repComment(Request $request){
        try{
            if($request->has('reply_content') && $request->has('reply_comment_id')){
                $dataArray = [];
                $dataArray['reply_user_id'] = $_COOKIE['user_id'];
                $dataArray['reply_content'] = $request->input('reply_content');
                $dataArray['reply_comment_id'] = $request->input('reply_comment_id');
                if($dataArray['reply_content'] != ""){
                    $reply = Reply::repComment($dataArray);
                    if($reply != null && $reply != false){
                        return $this->response(200,'回复成功！',$reply);
                    }
                    else if($reply == false){
                        return $this->response(304,'回复成功！返回错误！',$reply);
                    }
                    else{
                        return $this->response(304,'回复失败！',$reply);
                    }
                }
                else{
                    return $this->response(503,"回复内容不能为空！",null);
                }
            }
            else{
                return $this->response(503,"缺少必要参数！",null);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }

    public function likeReply(Request $request){
        try{
            if($request->has('reply_id')){
                $reply_id = $request->input('reply_id');
                $reply = new Reply($reply_id);
                $data = $reply->likeReply();
                if($data){
                    return $this->response(200,'点赞成功！',$data);
                }
                else{
                    return $this->response(304,'点赞失败！',$data);
                }
            }
            else{
                return $this->response(503,"缺少必要参数！",null);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }

    private function response($status,$message,$data){
        $response = [];
        $response['status'] = $status;
        $response['message'] = $message;
        $response['data'] = $data;
        return response()->json($response);
    }
}